<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="16">
            <a-form-item label="选择院校">
              <a-select allowClear v-model="queryParam.ssyxm" @change="yxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="16">
            <a-form-item label="课程">
              <a-select allowClear v-model="queryParam.kc" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.kc" :key="i.id" :value="i.kch">{{ i.kch + ' ' + i.kcmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="16">
              <a-form-item label="年级">
                <a-select allowClear v-model="queryParam.sznj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="16">
              <a-form-item label="课程类别">
                <a-select allowClear v-model="queryParam.kclb">
                  <a-select-option v-for="i in TYPE0359" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="18">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" style="margin:0 10px" @click="exportExcel">导出</a-button>
            <a-button @click="queryParam = {};$refs.table.refresh()" style="margin-left: 8px;">重置</a-button>

            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="{show: true}"
        :pageSize="parameter.pageSize"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <a slot="shtgrs" slot-scope="text, record" :disabled="text == 0" @click="showStu(record, '02')">{{ text }}</a>
        <a slot="shwtgrs" slot-scope="text, record" :disabled="text == 0" @click="showStu(record, '03')">{{ text }}</a>
        <a slot="wshtgrs" slot-scope="text, record" :disabled="text == 0" @click="showStu(record, '01')">{{ text }}</a>
        <a slot="zrs" slot-scope="text, record" :disabled="text == 0" @click="showStu(record)">{{ text }}</a>
      </s-table>
    </a-card>
    <stu-list ref="stuList" :visible="visible" @cancel="visible = false" @ok="visible = false"/>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import stuList from './stuList'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {},
      loading: false,
      list: [],
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' },
          width:80
        },
        {
          title: '课程代码',
          dataIndex: 'kcdm'
        },
        {
          title: '课程名称',
          dataIndex: 'kcmc'
        },
        {
          title: '审核通过人数',
          dataIndex: 'shtgrs',
          scopedSlots: { customRender: 'shtgrs' }
        },
        {
          title: '审核未通过人数',
          dataIndex: 'shwtgrs',
          scopedSlots: { customRender: 'shwtgrs' }
        },
        {
          title: '未审核人数',
          dataIndex: 'wshtgrs',
          scopedSlots: { customRender: 'wshtgrs' }
        },
        {
          title: '总人数',
          dataIndex: 'zrs',
          scopedSlots: { customRender: 'zrs' }
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.enrollmentStat(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      visible: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false
    }
  },
  components: {STable, stuList},
  computed: {
    ...mapState({
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0359: state => state.app['TYPE0359']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0359')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.initSelectMenu()
    // this.getStatis()
  },
  methods: {
    getStatis () {
      this.loading = true
      this.$api.cultivate.enrollmentStat(this.queryParam).then(res => {
        this.list = res.data.rows
      }).finally(() => {
        this.loading = false
      })
    },
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        this.$set(this.queryParam, 'sznj', res[1].currentGrade)
        this.initDone = true
      })
    },
    filterOption (value, yx) {
      return yx.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'kc', '')
      if (code) {
        this.$api.cultivate.courseAll({kcssyxm: code}).then(res => {
          this.$set(this.selectMenu, 'kc', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'kc', [])
      }
    },
    showDetail (t) {
      this.$router.push(`/cultivate/scheme/detail/${t.id}`)
    },
    exportExcel (){
      this.$export('/foster/pyjh//xkrstjExport', {
        ...this.queryParam
      })
    },
    showStu(record, shzt) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.stuList.params = {
          shzt,
          kcdm: record.kcdm,
          sznj: this.queryParam.sznj,
          ssyxm: this.queryParam.ssyxm,
          kc: this.queryParam.kc,
          kclb: this.queryParam.kclb
        }
        this.$refs.stuList.$refs.table.refresh(true)
      })
    }
  }
}
</script>

<style>

</style>
